# 打开浏览器
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from common.base import Base
from common.functions import open_browser
from page.longin_page import LoginPage


class Order(LoginPage, Base):
    def get_index(self):
        self.get("http://139.129.26.163")
        self.window()

    def login_index(self):
        # 登录
        self.click((By.LINK_TEXT, "请登录"))
        self.input_uesrname('fin9')
        self.input_password('123456')
        self.btn_login()

    def add_goods(self):
        # 点击商品
        self.click((By.XPATH, "//div[@class='all_ms']/a[4]"))
        # 点击立即购买按钮
        self.click((By.CLASS_NAME, "td1"))
        time.sleep(2)
        # 点击去结算按钮
        self.click((By.XPATH, "//a[@href='flow.php?step=checkout']"))

    def delivery_method1(self):
        # 申通快递
        self.click((By.XPATH, "//input[@name='shipping' and @value='5']"))

    def delivery_method2(self):
        # 邮局平邮
        self.click((By.XPATH, "//input[@name='shipping' and @value='6']"))

    def pay1(self):
        # 余额支付
        self.click((By.XPATH, "//input[@name='payment' and @value='1']"))

    def pay2(self):
        # 银行转账
        self.click((By.XPATH, "//input[@name='payment' and @value='2']"))

    def package1(self):
        # 不要包装
        self.click((By.XPATH, "//input[@name='pack' and @value='0']"))

    def package2(self):
        # 精品包装
        self.click((By.XPATH, "//input[@name='pack' and @value='1']"))

    def cardage1(self):
        # 不要贺卡
        self.click((By.XPATH, "//input[@name='card' and @value='0']"))

    def cardage2(self):
        # 需要贺卡
        self.click((By.XPATH, "//input[@name='card' and @value='1']"))

    def greetings(self):
        # 祝福语
        self.send_keys((By.NAME, "card_message"), '你好')

    def red_bages1(self):
        # 输入红包序列号
        self.send_keys((By.NAME, "bonus_sn"), '12346')
        # 验证红包
        self.click((By.XPATH, "//input[@value='验证红包']"))
        # 点击弹窗的确定按钮
        self.alert_operation()

    def bill1(self):
        # 点击发票
        self.click((By.NAME, "need_inv"))


    def bill2(self, index):
        # 选择下拉框
        self.select_By_index((By.NAME, 'inv_type'), index)

    def bill3(self):
        # 输入发票号
        self.send_keys((By.ID, "ECS_INVPAYEE"), "123456")

    def postscript(self):
        # 订单附言
        self.send_keys((By.ID, "postscript"), "谢谢你订购我们的产品,我们的产品一定会让不给你满意")

    def stock1(self):
        # 缺货处理:等所有商品备齐后再发
        self.click((By.XPATH, "//input[@name='how_oos' and @value='0']"))

    def stock2(self):
        # 缺货处理:取消订单
        self.click((By.XPATH, "//input[@name='how_oos' and @value='1']"))

    def stock3(self):
        # 缺货处理:与店主协商
        self.click((By.XPATH, "//input[@name='how_oos' and @value='2']"))

    def submit(self):
        # 点击提交订单
        self.click((By.XPATH, "//div[@class='flowBox']/div[2]/input[1]"))


if __name__ == '__main__':
    oe = Order(browser=open_browser())
    oe.get_index()
    oe.login_index()
    oe.add_goods()
    oe.delivery_method1()
    oe.delivery_method2()
    oe.pay2()
    oe.package2()
    oe.cardage2()
    oe.greetings()
    oe.red_bages1()
    # oe.bill(2)
    oe.postscript()
    oe.stock3()
    oe.submit()
